package com.nulldev.util.java.swing.extSwing;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.promise.Promiser;
import com.nulldev.util.promise.noresult.NoResultPromise;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ePasswordPane extends JFrame {
	
	public static interface ePasswordPaneEvent {
		/**
		 * @param password
		 * @returns boolean [TRUE = Password is correct, FALSE = Invalid password]
		 */
		public boolean onPassword(final String password);
		
		public void done(final String password);
		
		public void closed();
	}

	private JPanel contentPane;
	private JPasswordField passwordField;
	
	public static void main(String[] args) {
		main(new ePasswordPaneEvent() {
			
			@Override
			public boolean onPassword(String password) {
				return (password.equals("password123"));
			}
			
			@Override
			public void done(final String password) {
				JVMUtil.async_println("DONE");
			}

			@Override
			public void closed() {
				JVMUtil.async_println("CLOSED");
			}
		});
	}

	/**
	 * Launch the application.
	 */
	public static void main(final ePasswordPaneEvent events) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ePasswordPane frame = new ePasswordPane(events);
					ThemeApplier.applyTheme(frame);
					SwingUtil.centerFrame(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ePasswordPane(final ePasswordPaneEvent events) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				events.closed();
			}
		});
		setResizable(false);
		setTitle("ePasswordPane");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 337, 169);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblEnterYourPassword = new JLabel("Enter your password:");
		
		final JLabel lblInvalidPasswordTry = new JLabel("Invalid password, try again.");
		lblInvalidPasswordTry.setVisible(false);
		
		passwordField = new JPasswordField();
		passwordField.setEchoChar('*');
		
		final JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (passwordField.getPassword() == null || passwordField.getPassword().length == 0) {
					lblInvalidPasswordTry.setVisible(true);
					Promiser.promise(new NoResultPromise() {

						@Override
						public void run() throws Throwable {
							Thread.sleep(1000L);
							lblInvalidPasswordTry.setVisible(false);
						}

						@Override
						public void error(final Throwable throwable) {
							throwable.printStackTrace();
							lblInvalidPasswordTry.setVisible(false);
						}
						
					});
					return;
				}
				final String pwdInp = new String(passwordField.getPassword());
				if (pwdInp == null || pwdInp.length() == 0) {
					lblInvalidPasswordTry.setVisible(true);
					Promiser.promise(new NoResultPromise() {

						@Override
						public void run() throws Throwable {
							Thread.sleep(1000L);
							lblInvalidPasswordTry.setVisible(false);
						}

						@Override
						public void error(final Throwable throwable) {
							throwable.printStackTrace();
							lblInvalidPasswordTry.setVisible(false);
						}
						
					});
					return;
				}
				final boolean result = events.onPassword(pwdInp);
				if (result) {
					ePasswordPane.this.setVisible(false);
					ePasswordPane.this.dispose();
					events.done(pwdInp);
					return;
				}else {
					lblInvalidPasswordTry.setVisible(true);
					Promiser.promise(new NoResultPromise() {

						@Override
						public void run() throws Throwable {
							Thread.sleep(1000L);
							lblInvalidPasswordTry.setVisible(false);
						}

						@Override
						public void error(final Throwable throwable) {
							throwable.printStackTrace();
							lblInvalidPasswordTry.setVisible(false);
						}
						
					});
					return;
				}
			}
		});
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
						.addComponent(lblEnterYourPassword)
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(lblInvalidPasswordTry)
							.addPreferredGap(ComponentPlacement.RELATED, 149, Short.MAX_VALUE)
							.addComponent(btnSubmit)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblEnterYourPassword)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSubmit)
						.addComponent(lblInvalidPasswordTry))
					.addContainerGap(46, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
